package Servlet;

import Dao.outpatientDao;
import bean.OutpatientAppointment;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Comparator;
import java.util.List;

@WebServlet(urlPatterns = "/ManageQueue")
public class ManageQueue extends BaseServlet{

 outpatientDao dao = new outpatientDao();
 //生成排队队列的逻辑，先选出状态为排队中且已签到的预约记录列表，然后通过预约时间从早到晚排序，然后通过权值在进行排序，最后添加未签到的记录

 protected void generationQueue(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     //生成排队队列的逻辑，先选出状态为排队中且已签到的预约记录列表
     List<OutpatientAppointment> list = dao.queryRecordBYStatusAndSign("排队中", "已签到");
     list.sort(Comparator.comparing(OutpatientAppointment::getCreated_time));//按预约时间从早到晚排队
     list.sort(Comparator.comparing(OutpatientAppointment::getQueue_weight));//根据记录权值进行排队
     List<OutpatientAppointment> appointments = dao.queryRecordBYSign("未签到");//未签到的记录队列
     appointments.sort(Comparator.comparing(OutpatientAppointment::getCreated_time));//对未签到的记录队列排队
     for (OutpatientAppointment record : appointments) {
         list.add(record);//将未签到的队列排到队尾
     }
     HttpSession session = req.getSession();
     session.setAttribute("outpatient_queue",list);//将生成的排队队列存入session中

     //将队头病人设置到session域中
     session.setAttribute("head_patient",list.get(0));
     session.setAttribute("patient_list",list);//将病人排队队列存放在req域中
     req.getRequestDispatcher("/Hospital_call.jsp").forward(req,resp);

 }

 //医生叫号,若叫号是病人已经签到，则直接过号更新病人的预约状态为已完成，若未签到则将其移入队尾
    protected void call(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//        String rid = req.getParameter("rid");//通过预约记录号查找预约记录
//        OutpatientAppointment appointment = dao.queryRecordBYId(Integer.parseInt(rid));
        //判断病人是否签到
        HttpSession session = req.getSession();
        List<OutpatientAppointment> queue = (List<OutpatientAppointment>) session.getAttribute("outpatient_queue");//获取已经排好队的队列
        OutpatientAppointment record = queue.get(0);//取出队首病人预约记录，如果已签到，将签到状态改为已完成，直接过号，从session域中删除
        if(record.getSign_in_situation().equals("已签到")){
            dao.update_appointment_Status(record,"已完成");//更新病人预约状态为已完成
            queue.remove(record);//将jilucong队列中删除
        }else{
            queue.remove(record);//如果病人未签到，则将病人移入队尾，但这个逻辑有待商榷
            queue.add(record);
        }
        session.setAttribute("head_patient",queue.get(0));//更新第一个排队病人信息
        session.setAttribute("patient_list",queue);//更新session预约列表信息
//        session.setAttribute("head_patient",queue.get(0));
        resp.sendRedirect(req.getContextPath()+"/Hospital_call.jsp");//重定向到叫号页面
    }

    //获取医生选呼的对象，进行呼叫
    protected void selectCall(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        HttpSession session = req.getSession();
        List<OutpatientAppointment> queue = (List<OutpatientAppointment>) session.getAttribute("patient_list");//获取已经排好队的队列
        String outpatientId = req.getParameter("outpatient_id");
        OutpatientAppointment record = dao.queryRecordBYId(Integer.valueOf(outpatientId));
        if(record.getSign_in_situation().equals("已签到")){//如果病人预约状态为已签到
            dao.update_appointment_Status(record,"已完成");//更新病人预约状态为已完成
            queue.removeIf(appointment -> appointment.getOutpatient_id().equals(record.getOutpatient_id()));
        }else{
            req.setAttribute("msg",record.getPatient_name()+"病人尚未现场签到,已将其移入队尾");//若病人尚未签到，可以直接在页面上展示未签到，通过js技术判断，
            queue.removeIf(appointment -> appointment.getOutpatient_id().equals(record.getOutpatient_id()));
            queue.add(record);//添加记录zhiduiwei
            //直接可以选择别的病人叫号或者选择重呼
        }
        session.setAttribute("patient_list",queue);//更新session中的信息
        resp.sendRedirect(req.getContextPath()+"/Hospital_call.jsp");//重定向到叫号页面
    }
    //医生重呼
    protected void recall(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
    //医生过号
    protected void SignOver(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        HttpSession session = req.getSession();
        List<OutpatientAppointment> queue = (List<OutpatientAppointment>) session.getAttribute("patient_list");//获取已经排好队的队列
//        OutpatientAppointment record = queue.get(0);//取出队首病人预约记录，如果已签到，将签到状态改为已完成，直接过号，从session域中删除
        String recordId = req.getParameter("record_id");
        //通过预约记录号获取记录信息
        OutpatientAppointment record = dao.queryRecordBYId(Integer.valueOf(recordId));
        dao.update_appointment_Status(record,"已过号");//更新病人的预约状态为已过号
        queue.remove(0);//删除队头记录
        session.setAttribute("patient_list",queue);
        session.setAttribute("head_patient",queue.get(0));  //更新第一个排队病人信息
        req.setAttribute("message",record.getPatient_name()+"已被过号");//设置病人已过号的提醒信息
        resp.sendRedirect(req.getContextPath()+"/Hospital_call.jsp");
    }

    //点击选呼，获取选呼队列
    protected void record_list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            HttpSession session = req.getSession();
            List<OutpatientAppointment> queue = (List<OutpatientAppointment>) session.getAttribute("outpatient_queue");//获取已经排好队的队列
            req.setAttribute("patient_queue",queue);//获取整个队列的信息
            resp.sendRedirect("");
    }

    }
